package Persistence;

import java.sql.ResultSet;
import java.sql.SQLException;

import utilidades.leer;
import Dominion.Message;

public class MessageDAO extends CrudDAO<Message> {

	@Override
	public Message read(Message m) throws SQLException {
		String sqlQuery = "SELECT * FROM Article WHERE idArticle=" + m.getId() + "";
		ResultSet r = AgenteBD.getDbCon().query(sqlQuery);
		if(r != null){
			r.next();
			m.setBody(r.getString("body"));
			m.setDate(r.getString("send_date"));
			m.setEmail_from(r.getString("mail_from"));
			m.setEmail_to(r.getString("email_to"));
		}
		r.close();
		return m;
	}

	@Override
	public Message create(Message m) throws SQLException {
		String sqlQuery = "INSERT INTO Message (mail_from, mail_to, body, send_date) VALUES ("
							+ m.getEmail_from() + ","
							+ m.getEmail_to() + ","
							+ m.getBody() + ","
							+ leer.fecha() + ")";
							
		AgenteBD.getDbCon().queryUpdate(sqlQuery);
		return m;
	}

	@Override
	public void update(Message m) throws SQLException {
		String sqlQuery = "UPDATE Message SET "
							+ "mail_from=" + m.getEmail_from() + ","
							+ "mail_to=" + m.getEmail_to() + ","
							+ "body=" + m.getBody() + ","
							+ "send_date=" + m.getDate() + ") "
							+ "WHERE idMessage=" + m.getId() + "";
		
		AgenteBD.getDbCon().queryUpdate(sqlQuery);		
	}

	@Override
	public void delete(Message m) throws SQLException {
		String sqlQuery = "DELETE FROM Message WHERE idMessage=" + m.getId() + "";
		AgenteBD.getDbCon().queryUpdate(sqlQuery);
	}

}
